package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.action.background.TUsersAction;
import com.eportal.domain.Sysopt;
import com.eportal.params.background.TUsers.AuditParam;
import com.eportal.params.background.TUsers.AuthorizeKeysParam;
import com.eportal.params.background.TUsers.BatchUserRow;
import com.eportal.service.TUserApplyService;
import com.eportal.service.impl.TUserApplyServiceImpl;
import com.eportal.vo.controller.background.tusers.ApproveDetailVO;
import com.eportal.vo.controller.background.tusers.TUserApplyVO;
import com.framework.jqgrid.Page;
import com.framework.jqgrid.PageEntity;
import com.framework.utils.excel.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Saber on 2017/8/9.
 */
@Controller
@RequestMapping("/admin/tusers")
public class TUsersController {

    private static Logger logger = LoggerFactory.getLogger(TUserApplyServiceImpl.class.getName());

    @Resource
    private TUserApplyService tUserApplyService;

    /**
     * 申请钥匙审核列表
     * @author Saber
     * @Date 2017/8/9 下午2:26
     * @param request
     * @param communityId
     * @param agtCode
     * @param pageEntity    分页对象
     * @see TUsersAction#auditList()
     * @return
     */
    @RequestMapping(value = "auditList.json")
    @ResponseBody
    public JSONObject auditList(HttpServletRequest request,String communityId,String agtCode,PageEntity pageEntity){

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        JSONObject json = new JSONObject();

        try {
            PageInfo<TUserApplyVO> pageInfo=tUserApplyService.queryForPage(pageEntity,communityId,agtCode,user,userRole);

            json.put("total",pageInfo.getTotal());

            if(pageInfo.getList().size()==0){
                json.put("msg","暂无数据");
            }else{
                json.put("msg","ok");
            }
            json.put("data",pageInfo.getList());
            Page page = new Page(pageInfo,pageEntity.getPageNo());
            json.put("page", page);
            json.put("code",1);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("申请钥匙审核列表:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 住户审核-基本信息
     * @author Saber
     * @Date 2017/8/10 下午1:20
     * @param request
     * @param sId       T04_USERS_APPLY.SID
     * @see TUsersAction#approve()
     * @return
     */
    @RequestMapping(value = "getApproveDetail.json")
    @ResponseBody
    public JSONObject getApproveDetail(HttpServletRequest request,Long sId){
        JSONObject json = new JSONObject();
        try {
            ApproveDetailVO detail = tUserApplyService.getApplyDetail(sId);
            json.put("data",detail);
            json.put("code",1);
            json.put("msg","ok");
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("住户审核详情异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 住户审核
     * @author Saber
     * @Date 2017/8/10 下午3:38
     * @param request
     * @param param     参数
     * @see TUsersAction#audit()
     * @return
     */
    @RequestMapping(value = "audit.json",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject audit(HttpServletRequest request,AuditParam param){
        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");
        try {
            json=tUserApplyService.audit(param,user,userRole);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("住户审核异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
        return json;
    }

    /**
     * 预发钥匙
     * @author Saber
     * @Date 2017/8/15 下午1:43
     * @param request
     * @param param     参数
     * @see TUsersAction#authorizeKeys()
     * @return
     */
    @RequestMapping(value = "authorizeKeys.json",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject authorizeKeys(HttpServletRequest request,AuthorizeKeysParam param){

        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            param.setAppId(null);//管理端改参数为空
            json=tUserApplyService.authorizeKeys(param,user,userRole);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("发钥匙保存异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 预发钥匙-批量新增用户
     * @author Saber
     * @Date 2017/8/16 上午9:39
     * @param request
     * @param file  xls文件
     * @see TUsersAction#batchUser()
     * @return
     */
    @RequestMapping(value = "batchUser.json")
    @ResponseBody
    public JSONObject batchUser(HttpServletRequest request,@RequestParam(value = "file",required = true) MultipartFile file) {

        JSONObject json = new JSONObject();

        try {
            String sheetname  = "用户批量导入";
            ExcelUtil<BatchUserRow> importUtil = new ExcelUtil<BatchUserRow>(BatchUserRow.class);
            List<BatchUserRow> impData = importUtil.importExcel(sheetname, 1, file.getInputStream(),file.getOriginalFilename());
            json.put("code",1);
            json.put("msg","ok");
            json.put("data",impData);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("预发钥匙-批量导入异常",e.getMessage());
            json.put("code",0);
            json.put("msg","excel导入失败，查看格式是否正确");
            return json;
        }
    }
}
